package com.test.thread;

public class VolatileDemo {
    int a = 0;
    boolean flag = false;

    public void write() {
        flag = true;
        a = 1;
    }

    public void read() {
           if (flag) {
               System.out.println(a);
           }
    }

    public void test() throws InterruptedException {
        Thread a=new Thread(this::write);
        Thread b=new Thread(this::read);
        a.start();
        b.start();
    }

    public static void main(String[] args) throws InterruptedException {
        VolatileDemo demo=new VolatileDemo();
        demo.test();
    }

}
